package com.iseastar.guojiang.performance.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.guojiang.model.PerformanceBean;
import com.iseastar.guojiang.performance.MyPerformanceActivity;
import com.shouhuobao.station.R;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * Created by joker on 2017/8/2.
 * 我的业绩适配器
 */
public class PerformanceAdapter extends BaseAdapterExt<PerformanceBean> {

    MyPerformanceActivity activity = null;

    public PerformanceAdapter(ArrayList<PerformanceBean> items, Activity context) {
        super(items, context);
        activity = (MyPerformanceActivity) context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_performance_my, parent, false);
            holder.dateTV = (TextView) convertView.findViewById(R.id.dateTV);
            holder.rightIV = (ImageView) convertView.findViewById(R.id.rightIV);
            holder.takeExpressTV = (TextView) convertView.findViewById(R.id.take_express_TV);
            holder.entryExpressTV = (TextView) convertView.findViewById(R.id.entry_express_TV);
            holder.outExpressTV = (TextView) convertView.findViewById(R.id.out_express_TV);
            holder.performanceStatusIV = (ImageView) convertView.findViewById(R.id.performance_status_IV);
            holder.month_LL = (LinearLayout) convertView.findViewById(R.id.month_LL);
            holder.month_TV = (TextView) convertView.findViewById(R.id.month_TV);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final PerformanceBean item = items.get(position);
        holder.dateTV.setText(item.getDate());
        holder.takeExpressTV.setText("揽收  " + item.getCollectionCount());
        holder.entryExpressTV.setText("入库  " + item.getPushedCount());
        holder.outExpressTV.setText("出库  " + item.getPopedCount());
        if (item.getGrow() == -1) {
            holder.performanceStatusIV.setVisibility(View.VISIBLE);
            holder.performanceStatusIV.setImageResource(R.drawable.decline);
        } else if (item.getGrow()==0){
            holder.performanceStatusIV.setVisibility(View.GONE);
        }else {
            holder.performanceStatusIV.setVisibility(View.VISIBLE);
            holder.performanceStatusIV.setImageResource(R.drawable.up);
        }
        //用当前的年月和上一条数据的年月进行比较
        if (position == 0) {
            holder.month_LL.setVisibility(View.VISIBLE);
            holder.month_TV.setText(item.getDate().substring(0, 4) + "年" + item.getDate().substring(5, 7) + "月业绩");
        } else {
            if (items.get(position - 1).getDate().substring(0, 7).equals(item.getDate().substring(0, 7))) {
                holder.month_LL.setVisibility(View.GONE);
            } else {
                holder.month_LL.setVisibility(View.VISIBLE);
                holder.month_TV.setText(item.getDate().substring(0, 4) + "年" + item.getDate().substring(5, 7) + "月业绩");
            }
        }
        holder.rightIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onclick(item);
            }
        });
        return convertView;
    }

    private class ViewHolder {
        TextView dateTV;//日期
        ImageView rightIV;//进入详情页面
        TextView takeExpressTV;//揽收
        TextView entryExpressTV;//入库
        TextView outExpressTV;//出库
        ImageView performanceStatusIV;//业绩状态
        LinearLayout month_LL;
        TextView month_TV;
    }
}
